import sys, datetime, json, logging

from xpy3lib.utils import db_utils as util

from sicost.config import app_config
from xpy3lib.XLogger import XLogger
from xpy3lib.utils.XUtils import XUtils

from sicost.DP0101Job import DP0101Job
from sicost.DP0102Job import DP0102Job
from sicost.JHZKEYJob import JHZKEYJob
from sicost.CA0101Job import CA0101Job

profile = 'dev'


def singeCost(account, unit, cost_center, account_period_start, account_period_end, data_type):
    status = 0
    code = 0
    try:
        info = XUtils.get_git_info()
    except Exception as e:
        info = {'message': 'no_git'}

    level = logging.ERROR
    XLogger.getInstance(p_level=level, p_name_key_word='sicost').logger.info(
        '\n%s' % json.dumps(info, sort_keys=True, indent=4))

    #
    config = app_config[profile]

    try:
        db_conn_mpp = util.getConnectionDb2(config.DB_HOST_MPP_DB2,
                                            config.DB_PORT_MPP_DB2,
                                            config.DB_DBNAME_MPP_DB2,
                                            config.DB_USER_MPP_DB2,
                                            config.DB_PASSWORD_MPP_DB2)
        XLogger.getInstance().logger.info('connect db_conn_mpp success')

        db_conn_rds = util.getConnectionDb2(config.DB_HOST_RDS_DB2,
                                            config.DB_PORT_RDS_DB2,
                                            config.DB_DBNAME_RDS_DB2,
                                            config.DB_USER_RDS_DB2,
                                            config.DB_PASSWORD_RDS_DB2)
        XLogger.getInstance().logger.info('connect db_conn_rds success')

        db_conn_dbprod7 = util.getConnectionDb2(config.DB_HOST_DBPROD7_DB2,
                                                config.DB_PORT_DBPROD7_DB2,
                                                config.DB_DBNAME_DBPROD7_DB2,
                                                config.DB_USER_DBPROD7_DB2,
                                                config.DB_PASSWORD_DBPROD7_DB2)
        XLogger.getInstance().logger.info('connect db_conn_dbprod7 success')

    except Exception as e:
        db_conn_mpp = None
        db_conn_rds = None
        db_conn_dbprod7 = None
        XLogger.getInstance().logger.critical(str(e))

    #
    try:
        code = run_dp0102_job(p_config=config,
                       p_db_conn_mpp=db_conn_mpp,
                       p_db_conn_rds=db_conn_rds,
                       p_db_conn_dbprod7=db_conn_dbprod7,
                       p_data_type=data_type,
                       p_unit=unit,
                       p_account=account,
                       p_cost_center=cost_center,
                       p_account_period_start=account_period_start,
                       p_account_period_end=account_period_end
                       )
        if code == 0:
            return code
        run_dp0101_job(p_config=config,
                       p_db_conn_mpp=db_conn_mpp,
                       p_db_conn_rds=db_conn_rds,
                       p_db_conn_dbprod7=db_conn_dbprod7,
                       p_data_type=data_type,
                       p_unit=unit,
                       p_account=account,
                       p_cost_center=cost_center,
                       p_account_period_start=account_period_start,
                       p_account_period_end=account_period_end
                       )
        run_jhzkey_job(p_config=config,
                       p_db_conn_mpp=db_conn_mpp,
                       p_db_conn_rds=db_conn_rds,
                       p_db_conn_dbprod7=db_conn_dbprod7,
                       p_data_type=data_type,
                       p_unit=unit,
                       p_account=account,
                       p_cost_center=cost_center,
                       p_account_period_start=account_period_start,
                       p_account_period_end=account_period_end
                       )
        run_ca0101_job(p_config=config,
                       p_db_conn_mpp=db_conn_mpp,
                       p_db_conn_rds=db_conn_rds,
                       p_db_conn_dbprod7=db_conn_dbprod7,
                       p_data_type=data_type,
                       p_unit=unit,
                       p_account=account,
                       p_cost_center=cost_center,
                       p_account_period_start=account_period_start,
                       p_account_period_end=account_period_end
                       )
    except Exception as e:
        XLogger.getInstance().logger.critical(str(e))

    try:
        util.closeConnection(db_conn_mpp)
        util.closeConnection(db_conn_rds)
        util.closeConnection(db_conn_dbprod7)
    except Exception as e:
        XLogger.getInstance().logger.error(str(e))
        status = 1

    return code


def run_dp0101_job(p_config=None,
                   p_db_conn_mpp=None,
                   p_db_conn_rds=None,
                   p_db_conn_dbprod7=None,
                   p_data_type=None,
                   p_unit=None,
                   p_account=None,
                   p_cost_center=None,
                   p_account_period_start=None,
                   p_account_period_end=None):
    DP0101Job(p_config=p_config,
              p_db_conn_mpp=p_db_conn_mpp,
              p_db_conn_rds=p_db_conn_rds,
              p_db_conn_dbprod7=p_db_conn_dbprod7,
              p_unit=p_unit,
              p_account=p_account,
              p_cost_center=p_cost_center,
              p_account_period_start=p_account_period_start,
              p_account_period_end=p_account_period_end,
              p_data_type=p_data_type).execute()


def run_dp0102_job(p_config=None,
                   p_db_conn_mpp=None,
                   p_db_conn_rds=None,
                   p_db_conn_dbprod7=None,
                   p_data_type=None,
                   p_unit=None,
                   p_account=None,
                   p_cost_center=None,
                   p_account_period_start=None,
                   p_account_period_end=None):
    code=DP0102Job(p_config=p_config,
              p_db_conn_mpp=p_db_conn_mpp,
              p_db_conn_rds=p_db_conn_rds,
              p_db_conn_dbprod7=p_db_conn_dbprod7,
              p_unit=p_unit,
              p_data_type=p_data_type,
              p_account=p_account,
              p_cost_center=p_cost_center,
              p_account_period_start=p_account_period_start,
              p_account_period_end=p_account_period_end).execute()
    #print(code)
    return code


def run_jhzkey_job(p_config=None,
                   p_db_conn_mpp=None,
                   p_db_conn_rds=None,
                   p_db_conn_dbprod7=None,
                   p_data_type=None,
                   p_unit=None,
                   p_account=None,
                   p_cost_center=None,
                   p_account_period_start=None,
                   p_account_period_end=None):
    JHZKEYJob(p_config=p_config,
              p_db_conn_mpp=p_db_conn_mpp,
              p_db_conn_rds=p_db_conn_rds,
              p_db_conn_dbprod7=p_db_conn_dbprod7,
              p_unit=p_unit,
              p_account=p_account,
              p_cost_center=p_cost_center,
              p_account_period_start=p_account_period_start,
              p_account_period_end=p_account_period_end,
              p_data_type=p_data_type).execute()


def run_ca0101_job(p_config=None,
                   p_db_conn_mpp=None,
                   p_db_conn_rds=None,
                   p_db_conn_dbprod7=None,
                   p_data_type=None,
                   p_unit=None,
                   p_account=None,
                   p_cost_center=None,
                   p_account_period_start=None,
                   p_account_period_end=None):
    CA0101Job(p_config=p_config,
              p_db_conn_mpp=p_db_conn_mpp,
              p_db_conn_rds=p_db_conn_rds,
              p_db_conn_dbprod7=p_db_conn_dbprod7,
              p_unit=p_unit,
              p_account=p_account,
              p_cost_center=p_cost_center,
              p_account_period_start=p_account_period_start,
              p_account_period_end=p_account_period_end,
              p_data_type=p_data_type).execute()
